package com.lc.cehui.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.lc.cehui.R;
import com.lc.cehui.http.response.MyMemberShowBean;


import java.util.ArrayList;
import java.util.List;

public class BannerViewPagerAdapter extends PagerAdapter {

    private Context mContext;
    private int mChildCount = 0;
    private List<MyMemberShowBean.DataBean.GroupBean> mDatas = new ArrayList<>();
    private ViewPager viewPager;

    public BannerViewPagerAdapter(ViewPager viewPager) {
        this.viewPager = viewPager;
    }

    public void addData(List<MyMemberShowBean.DataBean.GroupBean> data) {
        if (null != data) {
            addData(data, true);
        }
    }

    public void addData(List<MyMemberShowBean.DataBean.GroupBean> data, boolean refresh) {
        if (refresh) {
            clear();
        }
        mDatas.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        if (mDatas != null) {
            mDatas.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * 覆盖getItemPosition()方法，当调用notifyDataSetChanged时，
     * 让getItemPosition方法人为的返回POSITION_NONE，
     * 从而达到强迫viewpager重绘所有item的目的。
     */
    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        if (mChildCount > 0) {
            mChildCount--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        if (mContext == null){
            mContext = container.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_card, container, false);
        TextView tv_name = view.findViewById(R.id.tv_name);
        TextView tv_content = view.findViewById(R.id.tv_content);

        TextView tv_money = view.findViewById(R.id.tv_money);
        tv_money.setText(mDatas.get(position).getPrice());
        TextView tv_receive = view.findViewById(R.id.tv_receive);
        RelativeLayout rl_bg = view.findViewById(R.id.rl_bg);
        if (mDatas.get(position).getGroup_id()==1){
            rl_bg.setBackgroundResource(R.drawable.viewpage1);
        }else if (mDatas.get(position).getGroup_id()==2){
            rl_bg.setBackgroundResource(R.drawable.viewpage2);
        }else {
            rl_bg.setBackgroundResource(R.drawable.viewpage3);
        }
        Log.e("TAG", "instantiateItem: "+mDatas.get(position).getTitle()+"");
        tv_name.setText(mDatas.get(position).getTitle()+"");

//        tv_text.setText(mDatas.get(position).getMoney()+"");

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(position);
            }
        });
        tv_receive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(mContext, ConsumeRecordActivity.class);
//                intent.putExtra("name", tv_name.getText().toString());
//                intent.putExtra("time", tv_time.getText().toString());
//                intent.putExtra("body", tv_text.getText().toString());
//                intent.putExtra("id",mDatas.get(position).getId());
//                mContext.startActivity(intent);
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
